<?php
class GiveBack
{
	public $borrowID;
	public $userID;
	public $bookID;

	public function __construct()
	{
		$this->borrowID = '';
		$this->userID = '';
		$this->bookID = '';
		$this->bookCode = '';
		$this->bookName = '';
	}

	
	public static function GetReturn($userID, $bookCode)
	{
		$giveback = new GiveBack();
		$sql = "select * from lib_borrowInfo
			where info_books_id =
			(
				select lib_books_id from lib_books
				where lib_books_code = '$bookCode'
			) and 
			info_user_id = $userID";

		$result = mysqli_query($GLOBALS['DB'], $sql);

		if(mysqli_num_rows($result) > 0){

			$row = mysqli_fetch_assoc($result);

			$giveback->borrowID = $row['info_borrow_id'];
			$giveback->userID = $userID;
			$giveback->bookID = $row['info_books_id'];
			$giveback->bookCode = "$bookCode";
			$giveback->bookName = $row['info_books_name'];

			mysqli_free_result($result);
			return $giveback;

		}else{
			mysqli_free_result($result);
			return false;
		}
	}

	public function ToRuturn($giveback)
	{	
		if($giveback)
		{	
		/**
		 * Step1: 删除lib_borrow中的相关借阅记录
		 */
			$sql_1 = "delete from lib_borrow 
			where 
			lib_borrow_id = $giveback->borrowID"; 

		/**
		 * Step2: 更新lib_books表中lib_book_nqty
		 */
			$sql_2 = "update lib_books set 
		 	lib_books_nqty = lib_books_nqty+1
		 	where
		 	lib_books_id = $giveback->bookID";

		/**
		 * Step3: 添加操作历史记录
		 */
			$sql_3 = "insert into lib_history values (
		 		NULL,
		 		'5',
		 		'$giveback->userID',
		 		'$giveback->bookID',
		 		now()
		 		)";

			mysqli_query($GLOBALS['DB'], $sql_1);
			mysqli_query($GLOBALS['DB'], $sql_2);
			mysqli_query($GLOBALS['DB'], $sql_3);

			return true;

		}else{
			return false;
		}
	} 
}
?>